package com.sniper.utils;

import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 校验工具类
 *
 * @author sniper
 */
public class ValidateUtil {

    public static boolean isValid(Object o) {
        return o != null;
    }

    /**
     * 验证字符串是否为空
     *
     * @param str
     * @return
     */
    public static boolean isValid(String str) {

        return !(str == null || "".equals(str.trim()));
    }

    /**
     * 验证整数是否为正整数
     *
     * @param i
     * @return
     */
    public static boolean isValid(Integer i) {
        return null != i;
    }

    public static boolean isValid(int i) {

        return i > 0;
    }

    /**
     * 集合验证
     *
     * @param cel
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static boolean isValid(Collection cel) {
        return !(cel == null || cel.isEmpty());
    }

    @SuppressWarnings("rawtypes")
    public static boolean isValid(Class[] paramType) {
        return paramType != null && paramType.length > 0;
    }

    public static boolean isValid(Object[] paramType) {
        return paramType != null && paramType.length > 0;
    }

    public static boolean isEmail(String email) {

        return ValidateUtil.isValid(email) && email.matches("^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$");

    }

    public static boolean isPhone(String phone) {

        return ValidateUtil.isValid(phone) && phone.matches("^1[34578]\\d{9}+$");

    }

    public static boolean isUrl(String url) {

        Pattern pattern = Pattern
                .compile(
                        "^(http|https|www|ftp|)?(://)?(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*((:\\d+)?)(/(\\w+(-\\w+)*))*(\\.?(\\w)*)(\\?)?(((\\w*%)*(\\w*\\?)*(\\w*:)*(\\w*\\+)*(\\w*\\.)*(\\w*&)*(\\w*-)*(\\w*=)*(\\w*%)*(\\w*\\?)*(\\w*:)*(\\w*\\+)*(\\w*\\.)*(\\w*&)*(\\w*-)*(\\w*=)*)*(\\w*)*)$",
                        Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(url);
        return matcher.matches();

    }

    public static boolean isInt(String s) {

        Pattern pattern = Pattern.compile("^-?\\d+$");
        Matcher matcher = pattern.matcher(s);
        return matcher.matches();

    }

}
